#include "util.hpp"
#include "config.hpp"
#include "datamanage.hpp"
#include "hot.hpp"
#include "service.hpp"
#include<thread>
void JsonUtilTest()
{
    const char *name = "zhangsan";
    int age = 18;
    float score[] = {89, 90, 99.5};
    Json::Value stu;
    stu["姓名"] = name;
    stu["年龄"] = age;
    stu["成绩"].append(score[0]);
    stu["成绩"].append(score[1]);
    stu["成绩"].append(score[2]);
    std::string str;
    Cloud::JsonUtil::Serialize(stu, &str);
    std::cout << str << std::endl;
    Json::Value val;
    Cloud::JsonUtil::Deserialize(str, &val);
    std::cout << val["姓名"].asString() << std::endl;
    std::cout << val["年龄"].asInt() << std::endl;
    for (int i = 0; i < val["成绩"].size(); i++)
    {
        std::cout << val["成绩"][i] << " ";
    }
}
void FileUtilTest()
{
    // cloud::FileUtil ul("./test2");
    // std::cout<<ul.FileName()<<std::endl;
    // std::cout<<ul.FileSize()<<std::endl;
    // std::cout<<ul.LastAtime()<<std::endl;
    // std::cout<<ul.LastMTime()<<std::endl;
    // ul.Compress("packed");
    // ul.UnCompress("unpacked");
    // ul.CreateDir();
    // std::vector<std::string> vs;
    // ul.ScanDir(&vs);
    // for(auto &u:vs)
    //{
    //     std::cout<<u<<std::endl;
    // }
}
void ConfigTest()
{
    Cloud::Config *config = Cloud::Config::GetInstance();
    std::cout << config->GetHotTime() << std::endl;
    std::cout << config->GetServerIp() << std::endl;
    std::cout << config->GetServerPort() << std::endl;
    std::cout << config->GetDownloadPrefix() << std::endl;
    std::cout << config->GetPackfileSuffix() << std::endl;
    std::cout << config->GetPackDir() << std::endl;
    std::cout << config->GetBackupDir() << std::endl;
    std::cout << config->GetBackupFile() << std::endl;
}
void DataTest(const std::string &filename)
{
    Cloud::DataManager data;
    std::vector<Cloud::BackupInfo> arr;
    data.GetAll(&arr);
    for (auto &a : arr)
    {
        std::cout << a.pack_flag << std::endl;
        std::cout << a.fsize << std::endl;
        std::cout << a.mtime << std::endl;
        std::cout << a.atime << std::endl;
        std::cout << a.real_path << std::endl;
        std::cout << a.pack_path << std::endl;
        std::cout << a.url << std::endl;
    }
    /*
    Cloud::BackupInfo info;
    info.SetBackupInfo(filename);
    Cloud::DataManager data;
    data.Insert(info);
    Cloud::BackupInfo tmp;
    data.GetOneByURL("/download/bundle.h", &tmp);
    std::cout << tmp.pack_flag << std::endl;
    std::cout << tmp.fsize << std::endl;
    std::cout << tmp.mtime << std::endl;
    std::cout << tmp.atime << std::endl;
    std::cout << tmp.real_path << std::endl;
    std::cout << tmp.pack_path << std::endl;
    std::cout << tmp.url << std::endl;
    Cloud::BackupInfo tmp2;
    data.GetOneByRealPath(filename, &tmp2);
    std::cout << tmp2.pack_flag << std::endl;
    std::cout << tmp2.fsize << std::endl;
    std::cout << tmp2.mtime << std::endl;
    std::cout << tmp2.atime << std::endl;
    std::cout << tmp2.real_path << std::endl;
    std::cout << tmp2.pack_path << std::endl;
    std::cout << tmp2.url << std::endl;
    info.pack_flag = true;
    data.Updata(info);
    std::vector<Cloud::BackupInfo> arr;
    data.GetAll(&arr);
    for (auto &a : arr)
    {
        std::cout << a.pack_flag << std::endl;
        std::cout << a.fsize << std::endl;
        std::cout << a.mtime << std::endl;
        std::cout << a.atime << std::endl;
        std::cout << a.real_path << std::endl;
        std::cout << a.pack_path << std::endl;
        std::cout << a.url << std::endl;
    }
    */
}
Cloud::DataManager *_data;
void HotTest()
{
    Cloud::HotManager hot;
    hot.RunModule();
}
void ServiceTest()
{
    Cloud::Service srv;
    srv.RunModule();
}
int main(int argc, char *argv[])
{
    _data = new Cloud::DataManager();
    // JsonUtilTest();
    // ConfigTest();
    // DataTest(argv[1]);
    // HotTest();
    std::thread thread_hot_manager(HotTest);
    std::thread thread_service(ServiceTest);
    thread_hot_manager.join();
    thread_service.join();
    return 0;
}
